package com.book.controller;

import com.book.entity.BookInfo;
import com.book.service.BookInfoService;
import com.book.util.JsonResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SuperYu  2024/7/30
 */

@Controller
@RequestMapping("/book")
@Slf4j
public class BookInfoController {
    @Resource
    BookInfoService bookInfoService;


    @GetMapping("/showByPage")
    public String showPage(int pageNum, int pageSize, Model model) {
        PageInfo<BookInfo> pageInfo = bookInfoService.findBookInfoByPage(pageNum, pageSize);

        model.addAttribute("page",pageInfo);

        return "admin/bookInfo/showBookInfo";
    }

    @GetMapping("/showAll")
    @ResponseBody
    public JsonResult<List<BookInfo>> showPage() {
        List<BookInfo> list = bookInfoService.findAllBookInfo();
        return JsonResult.ok(200, list);
    }


    @PostMapping("/add")
    @ResponseBody
    public JsonResult<String> showPage(@Valid BookInfo bookInfo, BindingResult result) {

        return null;
    }

    @PutMapping("/edit")
    @ResponseBody
    public JsonResult<String> edit( @RequestBody BookInfo bookInfo) {
        log.info("修改后的数据："+bookInfo);

        return JsonResult.ok(200,"成功");
    }


    @DeleteMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable int id) {
        System.out.println("要删除的账号id是：" + id);

        return "ok";
    }

    @GetMapping("/find/{id}")
    @ResponseBody
    public JsonResult<BookInfo> find(@PathVariable int id) {
        System.out.println("要修改的id是：" + id);

        BookInfo book = bookInfoService.findBookInfoById(id);

        return JsonResult.ok(200,book);
    }

    @GetMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();

        // System.out.println("要检查的用户名是：" + username);
        //
        // BookInfo bookInfo = bookInfoService.findBookInfoByLogin(username);

        if (true) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }
        return map;
    }

    @GetMapping("/checkNameByEdit")
    @ResponseBody
    public Map<String, Boolean> checkNameByEdit(int id, String bookName) {
        HashMap<String, Boolean> map = new HashMap<>();

        log.info("要检查的书名是：" + bookName + "   id是：" + id);

        BookInfo bookInfo = bookInfoService.findBookInfoByEdit(id, bookName);

        if (bookInfo == null) {

            map.put("valid", true);

        } else {

            map.put("valid", false);
        }
        return map;
    }

}
